﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;

namespace FormatUtil
{
    public class IdGen
    {
        static IdGen _inst = null;
        private readonly object _obj = new object();
        private Int32 _id = -1;
        public static IdGen Instance
        {
            get { return _inst ?? (_inst = new IdGen()); }
        }

        public int GenId()
        {
            lock (_obj)
            {
                if (_id >= 0xffff)
                {
                    _id = _id - 0xffff;
                }
                _id = _id + 1;
                return _id;
            }
        }
    }

    public class IntIdGener
    {
        bool _running = false;
        bool _exit = false;
        //int _serial = 0;

        object _sync = new object();

        static IntIdGener _inst = null;

        int _idGen = 0;

        public static IntIdGener Instance
        {
            get
            {
                if (_inst == null)
                {
                    _inst = new IntIdGener();
                }

                return _inst;
            }
        }

        public bool Exited
        {
            get
            {
                return _exit;
            }
        }

        public bool Running
        {
            get
            {
                return _running;
            }
        }


        private IntIdGener()
        {
        }


        public void Stop()
        {
            _running = false;
        }

        public int IdGen
        {
            get
            {
                //return GetAlarmInfoID();
                return Interlocked.Increment(ref _idGen);
            }
        }

    }
}
